/*
 * @Author: zll
 * @Date: 2024-06-11 17:15:40
 * @LastEditors: zll
 * @LastEditTime: 2024-06-24 08:17:20
 * @Description: file content
 * @FilePath: \erp-app\src\service\user.ts
 */
import { request } from '@/utils/request';
import { encryption, encryptionBase64, generateUUID } from '@/utils/util';

const basicAuth = `Basic ${encryptionBase64(import.meta.env.VITE_OAUTH2_APP_CLIENT)}`;

/** 登录 */
export const login = (query: { username: string; [key: string]: any }) => {
  const encPassword = encryption(query.password, import.meta.env.VITE_PWD_ENC_KEY);
  return request({
    url: `/auth/oauth2/token`,
    method: 'POST',
    header: {
      Authorization: basicAuth,
      'content-type': 'application/x-www-form-urlencoded',
    },
    query: {
      username: query.username,
      grant_type: 'password',
      scope: 'server',
      randomStr: generateUUID(),
    },
    data: {
      password: encPassword,
    },
  });
};

/** get 请求 */
export const queryUserInfo = () => {
  return request({
    url: `/admin/user/info`,
    method: 'GET',
  });
};

/** 保存clientId */
export const saveClientId = (data: any) => {
  return request({
    url: `/push/getui/user`,
    method: 'POST',
    header: {
      Authorization: basicAuth,
      'Content-Type': 'application/json; charset=UTF-8',
    },
    data,
  });
};
/** 退出登录调取接口清除个推绑定关系 */
export const ClearClientId = (cid: any) => {
  console.log(cid, 'cid');

  return request({
    url: `/push/getui/user/cid/${cid}`,
    method: 'DELETE',
  });
};

export const savePassword = (data: any) => {
  return request({
    url: `/admin/user/personal/password`,
    method: 'PUT',
    header: {
      Authorization: basicAuth,
      'Content-Type': 'application/json; charset=UTF-8',
    },
    data,
  });
};

export const registerPassword = (data: any) => {
  return request({
    url: `/admin/register/password`,
    method: 'POST',
    header: {
      Authorization: basicAuth,
      'Content-Type': 'application/json; charset=UTF-8',
    },
    data,
  });
};

export const getUserNameList = (params) => {
  return request({
    url: '/admin/user/getUserListByNickName',
    method: 'GET',
    data: params,
  });
};
